package com.aoip.telemetry.telemetry.configureui.common.file.controller;

import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.configureui.common.file.service.FileBaseService;
import com.aoip.telemetry.telemetry.configureui.common.utils.StringUtils;
import com.aoip.telemetry.telemetry.configureui.common.utils.file.FileUtils;
import com.aoip.telemetry.telemetry.core.config.RuoYiConfig;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.common.FileBaseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/2/16 15:35
 */
@RestController
@Slf4j
public class FileBaseController extends BaseController {
    @Resource
    private FileBaseService fileBaseService;

    /**
     * 通用下载请求
     *
     * @param id 文件编码
     */
    @PostMapping("/v1/POST/file/download/{id}")
    public void fileDownload(@PathVariable String id, HttpServletResponse response, HttpServletRequest request) {
        try {
            FileBaseEntity file = fileBaseService.file(id);
            if (file == null) {
                throw new Exception(StringUtils.format("文件不存在，请核实！ "));
            }
            String fileName = file.getFileName();
            String path = file.getFilePath();
//            String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
            path = path.replaceFirst("/profile", "");
            String filePath = RuoYiConfig.getProfile() + path;
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, fileName);
            FileUtils.writeBytes(filePath, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 通用预览请求
     *
     * @param id 文件编码
     */
    @PostMapping("/v1/GET/file/show/{id}")
    public AjaxResult fileShow(@PathVariable String id, HttpServletResponse response, HttpServletRequest request) {
        try {
            FileBaseEntity file = fileBaseService.file(id);
            if (file == null) {
                throw new Exception(StringUtils.format("文件不存在，请核实！ "));
            }
            String fileName = file.getFileName();
            String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
            String filePath = RuoYiConfig.getDownloadPath() + fileName;
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, realFileName);
            FileUtils.writeBytes(filePath, response.getOutputStream());
            return AjaxResult.success(fileName);
        } catch (Exception e) {
            log.error("下载文件失败", e);
            return AjaxResult.error("下载文件失败", e);
        }
    }

    /**
     * 根据文件id查询文件路径
     *
     * @param id 文件编码
     */
    @GetMapping("/v1/GET/file/path/{id}")
    public AjaxResult getFilePath(@PathVariable String id)  {
        FileBaseEntity file = fileBaseService.file(id);
        if (file == null) {
            return AjaxResult.error("文件不存在，请核实！ ");
        }
        String fileName = file.getFileName();
        String filePath = RuoYiConfig.getDownloadPath() + fileName;
        return AjaxResult.success(filePath);
    }
}
